package com.bluetooth;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import com.bean.User;
import com.service.UserService;
import com.util.Message;
import java.io.IOException;
import java.io.InputStream;
/**
 * 接收信息的线程类
 * Created by Administrator on 2017/9/26.
 */
public class AcceptThread extends Thread {
    private InputStream in;
    private Context context;
    private Handler handler=null;
    private StringBuilder builder=new StringBuilder();
    private double value;
    private boolean isExit=false;
    private boolean mode1;
    private boolean isSend=false;
    int time=0;
    private boolean message=true;
    public AcceptThread(InputStream in,Context context,Handler handler,double value)
    {
       this.in=in;
        this.context=context;
        this.value=value;
        this.handler=handler;
        mode1=true;
    }
    public void setValue(double value){
        this.value=value;

    }
    public AcceptThread(InputStream in,Context context ,Handler handler)
    {
        this.in=in;
        this.context=context;
        this.handler=handler;
    }
 private  void mode11ReadMessage()  {
     try {
         int count = 0;
         while (count == 0) {
             count = in.available();
         }
         byte[] bytes = new byte[count];
         in.read(bytes);
         String info = bytesToHexString(bytes);
         if(info.length()==2){
             if(info.equals("FF")){
                isSend=true;
                 builder.append("0.");
             }
             if(isSend&&!info.equals("FF")){
                 builder.append(info.charAt(1));
                 time++;
                 if(time==3){
                     time = 0;
                     if (handler != null) {
                         android.os.Message message = new android.os.Message();
                         message.what = 1;
                         message.obj = builder.toString();
                         handler.sendMessage(message);
                     }
                      float concentration = Float.parseFloat(builder.toString());
                     if (concentration > value) {
                     //发短信
                         User user = UserService.getSession(context);
                        if (message) {
                             com.util.Message.sendSMS(user.getPhone(), "【火灾通知】" + user.getAddress() + "发生了火灾");
                              message = false;
                          }
                    //  播放音乐
                          Message.playMusic(context);
                         Message.normalNotifaction(context);
                     }
                     builder.delete(0, builder.length());
                 }
             }
         }
//         if(info.length()==2) {
//             if (info.equals("FF")) {
//                 isSend = true;
//                 builder.append("0.");
//             }
            // if (isSend) {
              //   builder.append(info.charAt(1));
              //   time++;
//                 if (time == 3) {
//                     isSend = false;
//                     time = 0;
//                     if (handler != null) {
//                         android.os.Message message = new android.os.Message();
//                         message.what = 1;
//                         message.obj = builder.toString();
//                         handler.sendMessage(message);
//                     }
                    // float concentration = Float.parseFloat(builder.toString());
                     //if (concentration > value) {
                         //发短信
                     //    User user = UserService.getSession(context);
                      //   if (message) {
                     //        com.util.Message.sendSMS(user.getPhone(), "【火灾通知】" + user.getAddress() + "发生了火灾");
                    //         message = false;
                    //     }
                         // 播放音乐
                    //     Message.playMusic(context);
                         // 弹窗
                     //    Message.normalNotifaction(context);
                     //}
//                     builder.delete(0, builder.length());
//                     time = 0;
//                     Log.d("TEST", builder.toString());
                // }
             //}
        // }
//         if(info.length()==2&&time<3){
//             if(time==0) {
//                 builder.append("0." + info.charAt(1));
//             }else{
//                 builder.append(info.charAt(1));
//             }
//             time++;
//             if(time==3){
//                 if(handler!=null){
//                     Log.d("test",builder.toString());
//                     android.os.Message message=new android.os.Message();
//                     message.what=1;
//                     message.obj=builder.toString();
//                     handler.sendMessage(message);
//                 }
//                 float concentration=Float.parseFloat(builder.toString());
//                 if(concentration>value){
//                     //发短信
//                     User user= UserService.getSession(context);
//                     if(isSend) {
//
//                         com.util.Message.sendSMS(user.getPhone(),"【火灾通知】"+user.getAddress()+"发生了火灾");
//                         isSend=true;
//                     }
//                     // 播放音乐
//                     Message.playMusic(context);
//                     // 弹窗
//                     Message.normalNotifaction(context);
//                 }
//                 builder.delete(0,builder.length());
//                 time=0;
//             }
      // }
//

     } catch (Exception e) {
         e.printStackTrace();
     }

 }
  private  void model2ReadMessage()  {
      try {
          int count =1024;
          byte[] bytes = new byte[count];
          in.read(bytes);
           String info= new String(bytes, "utf-8");
          Log.d("test",info);
          if(handler!=null){
              android.os.Message message=new android.os.Message();
              message.what=0;
               message.obj=info;
              handler.sendMessage(message);
          }
          try {
              Thread.sleep(1000);

          }catch (Exception e){

          }
      } catch (IOException e) {
          e.printStackTrace();
      }

  }
    @Override
    public void run() {

        while(!isExit) {
            if(mode1) {
                mode11ReadMessage();
            }
            else{
                model2ReadMessage();
            }
        }
    }
    public void exit(){
        isExit=true;
    }
    public static String bytesToHexString(byte[] bytes) {
        String result = "";
        for (int i = 0; i < bytes.length; i++) {
            String hexString = Integer.toHexString(bytes[i] & 0xFF);
            if (hexString.length() == 1) {
                hexString = '0' + hexString;
            }
            result += hexString.toUpperCase();
        }
        return result;
    }
}
